<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加会员</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .user-form {
            max-width: 600px;
            margin: 20px auto;
            padding: 20px;
            background-color: white;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .user-form label {
            display: block;
            margin: 10px 0;
        }
        .user-form input {
            width: 96%;
            padding: 10px;
            font-size: 16px;
        }
        .user-form button {
            margin-top: 50px;
            display: block;
            width: 100%;
            padding: 10px;
            font-size: 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 5px;
        }
        .user-form button:hover {
            background-color: #0056b3;
        }
        a {
            display: block;
            text-align: center;
            margin-top: 20px;
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            text-decoration: none;
            border-radius: 5px;
        }
        a:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<h1>添加会员</h1>

<div class="user-form" >
    <form id="userForm" th:action="@{/users/data/addUser}" method="post">
        <input type="hidden" id="id" name="id" th:value="${user?.id}" required>
        <label for="name">姓名:</label>
        <input type="text" id="name" name="name" th:value="${user?.name}" required>
        <label for="username">昵称:</label>
        <input type="text" id="username" name="username" th:value="${user?.username}" required>
        <label for="password">密码:</label>
        <input type="text" id="password" name="password" th:value="${user?.password}" required>
        <label for="age">年龄:</label>
        <input type="number" id="age" name="age" th:value="${user?.age}" required>
        <label for="role">角色:</label>
        <input type="text" id="role" name="role" th:value="${user?.role}" required>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" th:value="${user?.email}" required>

        <button type="submit">提交</button>
    </form>

    <a th:href="@{/}" >返回</a>
</div>
</body>

<script>
    function getProjectPath() {
        const pathArray = window.location.pathname.split('/');
        const projectPath = '/' + pathArray[1];
        return projectPath;
    }
    document.getElementById('userForm').addEventListener('submit', function(event) {
        event.preventDefault(); // 阻止表单的默认提交行为

        // 获取表单元素
        var form = event.target;

        // 创建FormData对象来获取表单值
        var formData = new FormData(form);

        // 将FormData转换为JSON对象
        var json = {};
        formData.forEach(function(value, key){
            json[key] = value;
        });

        // 发送POST请求
        fetch(form.action, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(json)
        })
            .then(response => response.json()) // 假设服务器返回的是JSON格式的数据
            .then(data => {
                console.log('Success:', data);
                // 在这里处理成功的回调
                // window.location.href = '/';
                window.location.href = getProjectPath() + '/'; // 使用动态获取的项目路径
            })
            .catch(error => {
                console.error('Error:', error);
                // 在这里处理错误的回调
            });
    });
</script>
</html>
